<template>
	<view>
		<nav-header v-if="type=='school'" title="设置学校" :showRight="false"></nav-header>
		<nav-header v-else title="设置行业" :showRight="false"></nav-header>
		<view class="nickname">
			<view class="form">
				<view class="title">
					<text class="text-sm-Medium" v-if="type=='school'">我的学校</text>
					<text class="text-sm-Medium" v-else>我的行业</text>
					<text class="text-sm-Regular" style="color: #A4A7AE;" v-if="fontNum!=0">{{fontNum}}/30</text>
					<text class="text-sm-Regular" style="color: #A4A7AE;" v-else>{{countNickName}}/30</text>
				</view>
				<uni-forms ref="form" :modelValue="userInfo" :rules="rules">
					<uni-forms-item name="school" v-if="type=='school'">
						<uni-easyinput type="text" @input="sumFontNum" maxlength="30" v-model="userInfo.school" confirmType="search"
							clearable placeholder="请输入学校" :clearable="false" :inputBorder="false" @confirm="gethintList"/>
					    <scroll-view scroll-y class="content_hint" v-if="hintList.length>0">
							<view class="content_hint_text" v-for="(item,index) in hintList" :key="index"
							@click="selectSchool(item)">
							    {{item}}
							</view>
						</scroll-view>
					</uni-forms-item>
					<uni-forms-item name="occupation" v-else>
						<uni-easyinput type="text" @input="sumFontNum" maxlength="30" v-model="userInfo.occupation"
							clearable placeholder="请输入行业" :clearable="false" :inputBorder="false" confirmType="search" />
					</uni-forms-item>
				</uni-forms>
				<view class="tip">
					<text class="text-xs-Regular" v-if="type=='school'">*添加你的母校，找到更多校友。</text>
					<text class="text-xs-Regular" v-else>*分享你的职业领域，增加专业交流。</text>
				</view>
			</view>
			<view class="commit">
				<view class="button-style">
					<button class="btncolor" @click="submitForm()">
						<text class="text-md-Medium" style="color: #fff;">保存</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					school:"",
					occupation:""
				},
				hintList:[],
				type: null,
				rules: {
					school: {
						rules: [{
							required: true,
							errorMessage: '请输入学校',
						}],

					},
					school: {
						rules: [{
							required: true,
							errorMessage: '请输入行业',
						}],

					},
				},
				fontNum: 0,
				countNickName: 0
			}
		},
		methods: {
			submitForm() {
				this.$refs.form.validate().then(res => {
					this.$http({
						url: "/user/update",
						method: "PUT",
						data: this.userInfo
					}).then(() => {
						this.userStore.setUserInfo(this.userInfo);
						uni.showToast({
							title: "保存成功",
							icon: 'none'
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					})
				}).catch(err => {
					uni.showToast({
						title: "请输入内容",
						icon: "none"
					})
				})

			},
			selectSchool(item){
				this.hintList = []
				this.userInfo.school = item
				this.fontNum = this.userInfo.school.length
			},
			gethintList(){
                  this.$http({
						url: "/school/getSchoolName?schoolName="+this.userInfo.school,
						method: "get",
					}).then((res) => {
						this.hintList = res
					})
			},
			sumFontNum(e) { 
				this.fontNum = e.length
				
			}
		},
		onLoad(options) {
			this.type = options.type
			// 深拷贝一份数据
			let mine = this.userStore.userInfo;
			this.userInfo = JSON.parse(JSON.stringify(mine));
			this.$set(this.userInfo, 'isTmp', false)
			if (options.type == 'school') {
				this.countNickName = this.userInfo.school.length
				this.fontNum = this.userInfo.school.length
			} else {
				this.countNickName = this.userInfo.occupation.length
				this.fontNum = this.userInfo.occupation.length
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nickname {
		width: 100%;
        .content_hint{
			width: 100%;
			height: 300px;
			background-color: #fff;
			border: 1px solid gainsboro;
			.content_hint_text{
				padding: 10px;
			}
		}
		.form {
			display: flex;
			flex-direction: column;
			padding: 24rpx 32rpx;
			gap: 24rpx;

			:deep(.uni-forms) {
				height: 88rpx;
			}

			:deep(.uni-easyinput__content-input) {
				height: 40rpx;
				padding: 24rpx 32rpx;
				background-color: $gray-100;
			}

			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.tip {
				display: flex;
				align-items: center;
				justify-content: space-between;
				// font-size: 25rpx;
				color: $gray-400;
			}
		}



		.commit {
			width: 100%;
			position: fixed;
			bottom: 42rpx;
			left: 0;
			display: flex;
			justify-content: center;
			align-items: center;

			.button-style {
				padding: 24rpx 32rpx;
				width: 100%;
			}

			.btncolor {
				width: 100%;
				background-color: $brand-500;
				color: $white;
				border-radius: 34rpx;
				padding: 8rpx 24rpx;
				line-height: 60rpx;
				text-align: center;
			}
		}
	}
</style>